import { XfVoiceDictation } from '@muguilin/xf-voice-dictation'

let xfVoiceInstance = null
let times = null

const createXfVoiceInstance = (onTextChangeCallback) => {
  xfVoiceInstance = new XfVoiceDictation({
    APPID: '0bc63f8f',
    APISecret: 'ZjcyYTgzY2U4YzA4MDBhNWMxMjVkMTAz',
    APIKey: '97b9d6f86ce4832e9b0ca7b966d8ad14',

    onWillStatusChange: function (oldStatus, newStatus) {
      // 状态变化处理逻辑
    },

    onTextChange: function (text) {
      if (text) {
        clearTimeout(times)
        times = setTimeout(() => {
          if (xfVoiceInstance) {
            xfVoiceInstance.stop()
          }
        }, 3000)
      }
      onTextChangeCallback(text) // 调用回调函数
    },

    onError: function (error) {
      console.log('错误信息：', error)
    },
  })

  return xfVoiceInstance
}

export default createXfVoiceInstance
